package cn.fl.capital.service.impl;

import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.DecimalUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.capital.constant.CapitalConstant;
import cn.fl.capital.service.ICapFundApiService;
import cn.fl.capital.service.ICapFundInsIncomeService;
import cn.fl.capital.transform.CapFundVOTransform;
import cn.fl.capital.vo.CapFundVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.insurance.vo.InsCapFundVO;
import fl.customer.facade.customer.api.ICustomerQueryApiService;
import ins.car.facade.car.car.api.IInsQueryApiService;
import ins.car.facade.car.car.condition.InsPayInfoQC;
import ins.car.facade.car.car.dto.InsPayInfoDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CapFundInsIncomeServiceImpl implements ICapFundInsIncomeService {

    @Autowired
    private IInsQueryApiService insQueryApiService;
    @Autowired
    private ICustomerQueryApiService customerQueryApiService;
    @Autowired
    private ICapFundApiService capFundApiService;

    @Override
    public boolean batchAddImport(List<InsCapFundVO> list) throws BaseException {
        InsCapFundVO insCapFundVO = null;
        InsPayInfoQC insPayInfoQC = null;
        InsPayInfoDTO insPayInfoDTO = null;
        List<InsPayInfoDTO> insDtos = null;
        CapFundVO vo = null;
        List<CapFundVO> capFunlist = new ArrayList<CapFundVO>();
        for (int i = 0; i < list.size(); i++) {
            insCapFundVO = list.get(i);

            insPayInfoQC = new InsPayInfoQC();
            insPayInfoQC.setPayNo(insCapFundVO.getPayNo());
            insDtos = insQueryApiService.searchInsPayInfoList(insPayInfoQC);
            if (CollectionUtils.isEmpty(insDtos)) {
                throw new BaseException("第" + (i + 1) + "行数据的支付表号：" + insCapFundVO.getPayNo() + "，在保险支付表中不存在！请检查文件");
            } else {
                insPayInfoDTO = insDtos.get(0);
                if (insPayInfoDTO == null || StringUtils.isBlank(insPayInfoDTO.getPayNo())) {
                    throw new BaseException("第" + (i + 1) + "行数据的支付表号：" + insCapFundVO.getPayNo() + "，在保险支付表中不存在！请检查文件");
                }
            }

            if (insPayInfoDTO.getCustomerId() == null) {
                throw new BaseException("第" + (i + 1) + "行数据在保险支付表中的客户号为空，请检查文件。");
            }

            vo = new CapFundVO();
            vo.setCstId(insPayInfoDTO.getCustomerId());
            vo.setCstNm(insPayInfoDTO.getCustomerNm());
            vo.setAcceptCode(insCapFundVO.getAcceptCode());
            vo.setAcceptName(insCapFundVO.getAcceptName());
            vo.setComeCode(insCapFundVO.getComeCode());
            vo.setComeName(insCapFundVO.getComeName());
            vo.setDocketRem(insCapFundVO.getDocketRem());
            vo.setBusinessCode(insCapFundVO.getPayNo());
            vo.setAmount(DecimalUtils.convertYuanToFen(insCapFundVO.getAmount()));
            vo.setAvailableAmount(DecimalUtils.convertYuanToFen(insCapFundVO.getAmount()));
            vo.setUsedAmount(0L);
            // 资金状态集合 CAP0004：未分解
            vo.setFundStatusCd(CapitalConstant.CODE_CAP_FUND_DECO_U);
            // 资金来源CAP0002：正常来款
            vo.setFundSourcesCd(CapitalConstant.CODE_CAP_FUND_SRC_C);
            // 资金录入方式：2.批量导入
            vo.setFundInputCd(CapitalConstant.CODE_CAP_FUND_INPUT_CD_2);
            // 资金种类CAP0014：1-保险租金
            vo.setFundTypCd(CapitalConstant.CODE_CAP_FUND_TYP_CD_1);
            // 实际到账日期=交易时间
            vo.setAcceptActualDate(insCapFundVO.getAcceptDate());
            vo.setAcceptDate(insCapFundVO.getAcceptDate());
            vo.setCrtTm(new Date());
            vo.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
            vo.setIdentifyOperator(CurrentThreadContext.getCurrentUserId());
            vo.setIdentifyOperatorNm(CurrentThreadContext.getCurrentUserName());
            vo.setIdentifyTime(new Date());
            capFunlist.add(vo);
        }

        if (CollectionUtils.isNotEmpty(capFunlist)) {
            return this.capFundApiService.addCapFund(CapFundVOTransform.toDTOList(capFunlist));
        }
        return false;
    }
}
